﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson01
{
    public class CollisionGame : Game
    {
        private Random _random = new Random();
        private SpriteBatch _spriateBatch;

        private Texture2D _textureMain;
        private Texture2D _textureItem1;
        private Texture2D _textureItem2;
        private Texture2D _textureThreeRings;
        private Texture2D _textureTree;

        private List<Texture2D> _monstersTextures = new List<Texture2D>();

        private Vector2 _centerPosition = Vector2.Zero;
        private Vector2 _position1 = Vector2.Zero;
        private Vector2 _position2 = Vector2.Zero;

        private Vector2 _speed1 = new Vector2(2f, 2f);
        private Vector2 _speed2 = new Vector2(3f, 3f);

        private Vector2 _frameSize = new Vector2(75, 75);
        private Vector2 _currentFrame = new Vector2(0, 0);
        private Vector2 _sheetSize = new Vector2(6, 8);

        int _timeSinceLastFrame;
        int _millisecondsPerFrame = 100;

        private int _currentMonsterFrame;

        public CollisionGame()
        {
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            _spriateBatch = new SpriteBatch(GraphicsDevice);
            _textureMain = Content.Load<Texture2D>(@"Images/GUI/menu/baseframe");
            _textureItem1 = Content.Load<Texture2D>(@"Images/GUI/menu/btn_effects");
            _textureItem2 = Content.Load<Texture2D>(@"Textures/Crosshair");
            _textureThreeRings = Content.Load<Texture2D>(@"Images/threerings");
            _textureTree = Content.Load<Texture2D>(@"Textures/Tree");

            for (int index = 1; index <= 45; index++)
            {
                _monstersTextures.Add(Content.Load<Texture2D>(string.Format(@"Textures/0010/{0}", index)));
            }
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            _spriateBatch = null;
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Escape))
            {
                Exit();
            }
            //_position1 += _speed1;
            //if (_position1.X > (GraphicsDevice.Viewport.Width - _textureItem1.Width) || _position1.X < 0)
            //{
            //    _speed1.X *= -1;
            //}
            //if (_position1.Y > (GraphicsDevice.Viewport.Height - _textureItem1.Height) || _position1.Y < 0)
            //{
            //    _speed1.Y *= -1;
            //}

            //_position2 += _speed2;
            //if (_position2.X > (GraphicsDevice.Viewport.Width - _textureItem2.Width) || _position2.X < 0)
            //{
            //    _speed2.X *= -1;
            //}
            //if (_position2.Y > (GraphicsDevice.Viewport.Height - _textureItem2.Height) || _position2.Y < 0)
            //{
            //    _speed2.Y *= -1;
            //}

            _timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (_timeSinceLastFrame > _millisecondsPerFrame)
            {
                _timeSinceLastFrame -= _millisecondsPerFrame;
                ++_currentFrame.X;
                if (_currentFrame.X >= _sheetSize.X)
                {
                    _currentFrame.X = 0;
                    ++_currentFrame.Y;
                    if (_currentFrame.Y >= _sheetSize.Y)
                    {
                        _currentFrame.Y = 0;
                    }
                }
                _currentMonsterFrame++;
                if(_currentMonsterFrame >= 45)
                {
                    _currentMonsterFrame = 0;
                }
            }

            _centerPosition.X = (float) (ActualWidth / 2);
            _centerPosition.Y = (float)(ActualHeight / 2);
            //_centerPosition.X = (float)(ActualWidth * _random.NextDouble());
            //_centerPosition.Y = (float)(ActualHeight * _random.NextDouble());

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.Black);
            _spriateBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            _spriateBatch.Draw(_textureMain, _centerPosition, null, Color.White, 0, new Vector2(_textureMain.Width / 2.0f, _textureMain.Height / 2.0f), 1.0f, SpriteEffects.None, 0.1f);
            _spriateBatch.Draw(_textureItem1, _centerPosition, null, Colors.White, 0, new Vector2(_textureItem1.Width / 2.0f, _textureItem1.Height / 2.0f), 1.0f, SpriteEffects.None, 0.6f);
            _spriateBatch.Draw(_textureItem2, _centerPosition, null, Colors.White, 0, new Vector2(_textureItem2.Width / 2.0f, _textureItem2.Height / 2.0f), 1.0f, SpriteEffects.None, 0.7f);
            //_spriateBatch.Draw(_textureTree, _centerPosition, null, new Color(255, 255, 255, 192), 0, new Vector2(_textureTree.Width / 2.0f, _textureTree.Height / 2.0f), 1.0f, SpriteEffects.None, 0.9f);
            _spriateBatch.Draw(_textureThreeRings, _centerPosition, new Rectangle((int)(_currentFrame.X * _frameSize.X), (int)(_currentFrame.Y * _frameSize.Y), (int)_frameSize.X, (int)_frameSize.Y), new Color(255, 255, 255, 192), 0, new Vector2(_frameSize.X / 2, _frameSize.Y / 2), 1.0f, SpriteEffects.None, 1f);

            _spriateBatch.Draw(_monstersTextures[_currentMonsterFrame], _centerPosition, null, Colors.CornflowerBlue, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.5f);
            _spriateBatch.End();

            base.Draw(gameTime);
        }
    }
}